import datetime
import db
import KBEDebug as log
import KBEngine
import time

class LobbyInviteInfo:
    def __init__(self, table_name):
        self.table_name = table_name
        self.tb = db.DBTable(table_name)
        # 邀请后缀
        self.tb.setFiled('playerId', db.UINT32())
        # 邀请人数
        self.tb.setFiled('count', db.UINT32())

    def createTable(self, success_callback):
        self.tb.exec(success_callback)


class ILobbyInviteInfo:
    def __init__(self):
        self.tb_lobbyInviteInfo = LobbyInviteInfo('lobby_invite_info')
        self.tb_create_count = 1
        self.tb_lobbyInviteInfo.createTable(self.createTableSuccess)

    def createTableSuccess(self):
        self.tb_create_count -= 1
        if self.tb_create_count <= 0:
            pass

    def changeLobbyInviteRecord(self, playerId):
        def callback(record):
            if len(record) == 0:
                # 创建一条新记录
                info = {
                    'playerId': playerId,
                    'count': 1
                }
                self.tb_lobbyInviteInfo.tb.addLine(info)
                log.DEBUG_MSG(f'创建lobby邀请信息: {playerId}')
            else:
                # 记录已存在，增加数量
                sql = f'UPDATE lobby_invite_info SET count = count + 1 WHERE playerId = \"{playerId}\"'
                log.DEBUG_MSG(f'更新lobby登录记录: {playerId}')
                KBEngine.executeRawDatabaseCommand(sql, None)
        
        self.tb_lobbyInviteInfo.tb.loadAll(f'playerId=\"{playerId}\"', callback)
